﻿

namespace Mulala.Cavan.Application.MainBoundedContext.CorporationModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.Seedwork;
    using Spring.Context;
    using Spring.Context.Support;

    public class CorporationNewAppService : BaseAppService<CorporationNew, CorporationNewSearch, ICorporationNewRepository>, ICorporationNewAppService
    {
        private IApplicationContext SpringContext = null;
        private ICorporationAppService CorporationAppService { get; set; }

        public override BoolGenericResult<CorporationNew> Add(CorporationNew newCorporationNew)
        {
            this.SpringContext = ContextRegistry.GetContext();
            this.CorporationAppService = this.SpringContext.GetObject("CorporationAppService") as ICorporationAppService;

            BoolGenericResult<CorporationNew> result = new BoolGenericResult<CorporationNew>();

            Corporation corporation = this.CorporationAppService.GetById(newCorporationNew.Corporation.ID);

            result = corporation.PublishNew(newCorporationNew.Title, newCorporationNew.Content, newCorporationNew.Type);

            if (result.IsSuccess)
            {
                result.Messages.Add("保存成功！");
                this.Repository.Save(result.Object);
            }

            return result;
        }

        public override BoolGenericResult<CorporationNew> Update(CorporationNew changedCorporationNew)
        {
            this.SpringContext = ContextRegistry.GetContext();
            this.CorporationAppService = this.SpringContext.GetObject("CorporationAppService") as ICorporationAppService;

            BoolGenericResult<CorporationNew> result = new BoolGenericResult<CorporationNew>();

            Corporation corporation = this.CorporationAppService.GetById(changedCorporationNew.Corporation.ID);
            CorporationNew currentCorporationNew = this.GetById(changedCorporationNew.ID);

            result = corporation.ModifyNew(currentCorporationNew, changedCorporationNew.Title, changedCorporationNew.Content, changedCorporationNew.Type);

            if (result.IsSuccess)
            {
                result.Messages.Add("保存成功！");
                this.Repository.Save(result.Object);
            }

            return result;
        }
    }
}
